<template>
  <div>
    <!-- nav栏 -->
    <van-nav-bar
      title="标题"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        name="mobile"
        label="phone"
        placeholder="手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对',
          },
        ]"
      />
      <van-field
        v-model="code"
        type="code"
        name="code"
        label="验证码"
        placeholder="验证码"
        required
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /\d{6}/, message: '验证码格式不对' },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLogin"
          :disabled="isLogin"
          loading-text="加载中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from "./user.js";
import { saveToken, getToken, removeToken } from "@/utils/token.js";
export default {
  name: "login",
  data() {
    return {
      phone: "",
      code: "246810",
      isLogin: false,
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      this.isLogin = true;
      userLogin(values)
        .then((res) => {
          console.log(res);
          this.$toast.success("登录成功");
          //   console.log();
          saveToken(res.data.data);
          console.log(getToken());
          this.$router.push({ path: "/" });
        })
        .catch((err) => {
          console.log(err);
          this.$toast.fail("账号或验证码错误");
        })
        .then(() => {
          this.isLogin = false;
        });
    },
  },
};
</script >


<style lang="less">
.van-nav-bar__content {
  background-color: #0098fa;
  .van-nav-bar__title {
    color: #fff;
  }
}
.van-nav-bar__left {
  .van-icon::before,
  .van-nav-bar__text {
    color: #fff;
  }
}
</style>